from pydantic import BaseModel

from backend.app.app.db.session_public import session_pub
import backend.app.app.models.db_owi_public_models as  md_pub
from backend.app.app.models.dvpunithist import data_save

#    读 ID
def get_u_id():
    try:
        result = session_pub.query(md_pub.TbYearProfitLossAn.user_id).all()
        return result
    except:
        print('查找失败')
        session_pub.rollback()
        return False

#    读 pro_ID
def get_p_id():
    try:
        result = session_pub.query(md_pub.TbYearProfitLossAn.project_id).all()
        return result
    except:
        print('查找失败')
        session_pub.rollback()
        return False

#   根据user_id查询pro_id
def get_p_id_u(user_id):
    try:
        result = session_pub.query(md_pub.TbYearProfitLossAn.project_id).filter(md_pub.TbYearProfitLossAn.user_id == user_id).all()
        return result
    except:
        print('查找失败')
        session_pub.rollback()
        return False

#   读 页面数据
def query_data(user_Id,Pro_Id):
    try:
        project_inf = session_pub.query(md_pub.TbYearProfitLossAn).filter(
            md_pub.TbYearProfitLossAn.user_id == user_Id,
            md_pub.TbYearProfitLossAn.project_id == Pro_Id
        ).first()
        return project_inf.__dict__
    except:
        print('查找失败')
        return False

#   更新数据
def updata_project_db(s_data:data_save):
    try:
        db_data = session_pub.query(md_pub.TbYearProfitLossAn).filter(
            md_pub.TbYearProfitLossAn.user_id == s_data.User_ID,
            md_pub.TbYearProfitLossAn.project_id == s_data.Proj_ID
        )
        if db_data.first():
            db_data.update({
                'dvpUnit_Name': s_data.dvpUnit_Name,
                'stimType_Name': s_data.stimType_Name,
                'year': s_data.year,
                'calcu_type': s_data.calculation_type,
                'analysis_data': s_data.analysis_data,
                'parm_set_data': s_data.parameter_set,
                'OilPriceCritical': s_data.OilPriceCritical,
                'IncreOilCritical': s_data.IncreOilCritical,
                'StimCostCritical': s_data.StimCostCritical,
                'Stim_WellNumber_Valid': s_data.Stim_WellNumber_Valid,
                'Stim_WellValidRatio': s_data.Stim_WellValidRatio,
                'Stim_ValidPeriod_Average': s_data.Stim_ValidPeriod_Average,
                'Stim_IncreOil_Average': s_data.Stim_IncreOil_Average,
                'StimOil_CommodityRatio': s_data.StimOil_CommodityRatio,
                'Oil_Price': s_data.Oil_Price,
                'OilSale_TotalTaxRatio': s_data.OilSale_TotalTaxRatio,
                'Money_Discount': s_data.Money_Discount,
                'Oil_OprCost': s_data.Oil_OprCost,
                'StimCost_Average':  s_data.StimCost_Average
            })
        else:
            session_pub.add(md_pub.TbYearProfitLossAn(
                user_id=s_data.User_ID,
                project_id=s_data.Proj_ID,
                dvpUnit_Name=s_data.dvpUnit_Name,
                stimType_Name=s_data.stimType_Name,
                year=s_data.year,
                calcu_type=s_data.calculation_type,
                analysis_data=s_data.analysis_data,
                parm_set_data=s_data.parameter_set,
                OilPriceCritical=s_data.OilPriceCritical,
                IncreOilCritical=s_data.IncreOilCritical,
                StimCostCritical=s_data.StimCostCritical,
                Stim_WellNumber_Valid=s_data.Stim_WellNumber_Valid,
                Stim_WellValidRatio=s_data.Stim_WellValidRatio,
                Stim_ValidPeriod_Average=s_data.Stim_ValidPeriod_Average,
                Stim_IncreOil_Average=s_data.Stim_IncreOil_Average,
                StimOil_CommodityRatio=s_data.StimOil_CommodityRatio,
                Oil_Price=s_data.Oil_Price,
                OilSale_TotalTaxRatio=s_data.OilSale_TotalTaxRatio,
                Money_Discount=s_data.Money_Discount,
                Oil_OprCost=s_data.Oil_OprCost,
                StimCost_Average=s_data.StimCost_Average
                ))
        session_pub.commit()
        return True
    except Exception as e:
        session_pub.rollback()
        print("更新数据失败", e, e.__traceback__.tb_lineno)
        return False